package com.info.zhiduoduo.weixin.service;

import com.baomidou.mybatisplus.mapper.Condition;
import com.info.zhiduoduo.common.constants.SmartResult;
import com.info.zhiduoduo.common.constants.StatusMsg;
import com.info.zhiduoduo.common.constants.system.UserType;
import com.info.zhiduoduo.common.formbean.BaseSimpleFormBean;
import com.info.zhiduoduo.common.utils.NumberToCN;
import com.info.zhiduoduo.common.utils.NumberUtil;
import com.info.zhiduoduo.core.service.CommonService;
import com.info.zhiduoduo.core.service.WxMessageService;
import com.info.zhiduoduo.core.service.common.BaseService;
import com.info.zhiduoduo.repository.entity.company.Company;
import com.info.zhiduoduo.repository.entity.company.CompanyAccount;
import com.info.zhiduoduo.repository.entity.crowdsource.CrowdsourceAccount;
import com.info.zhiduoduo.repository.entity.evaluate.Evaluate;
import com.info.zhiduoduo.repository.entity.maker.MakerAccount;
import com.info.zhiduoduo.weixin.formbean.evaluate.SaveEvaluateFormBean;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Transactional
@Service
public class EvaluateService extends BaseService {

	@Resource
	private WxMessageService wxMessageService;
	@Resource
	private CommonService commonService;

	@Transactional
	public SmartResult saveEvaluate(BaseSimpleFormBean<SaveEvaluateFormBean> formBean) {
		if (null == formBean || null == formBean.getReqdata() || 0 == formBean.getUserid()) {
			return errorParamSmartResult();
		}
		SaveEvaluateFormBean evaluateFormBean = formBean.getReqdata();
		//判断评价者用户类型
		if (NumberUtil.intIn(evaluateFormBean.getEvaluatorUserType(), UserType.MAKER, UserType.TAXER, UserType.MAKER_CROWDSOURCE)) {
			//获取创客
			MakerAccount account = new MakerAccount().selectOne(Condition.create().eq("userid", evaluateFormBean.getEvaluatorUserId()));
			if (null == account) {
				return smartResult(StatusMsg.StatusMsg_201);
			}
			Evaluate evaluate = new Evaluate();
			BeanUtils.copyProperties(evaluateFormBean, evaluate);
			if (!evaluate.insert()) {
				return smartResult(StatusMsg.StatusMsg_199);
			}
			if (0 == account.getWhetherEvaluate()) {
				//将是否评价过标识修改
				account.setWhetherEvaluate(1);
				if (!account.updateById()) {
					return smartResult(StatusMsg.StatusMsg_199);
				}
			}

		} else if (UserType.COMPANY == evaluateFormBean.getEvaluatorUserType()) {
			//获取企业
			CompanyAccount account = new CompanyAccount().selectOne(Condition.create().eq("compid",
					evaluateFormBean.getEvaluatorUserId()));
			if (null == account) {
				return smartResult(StatusMsg.StatusMsg_302);
			}
			Evaluate evaluate = new Evaluate();
			BeanUtils.copyProperties(evaluateFormBean, evaluate);
			if (!evaluate.insert()) {
				return smartResult(StatusMsg.StatusMsg_199);
			}
			if (0 == account.getWhetherEvaluate()) {
				account.setWhetherEvaluate(1);
				if (!account.updateById()) {
					return smartResult(StatusMsg.StatusMsg_199);
				}
			}

		} else if (NumberUtil.equals(evaluateFormBean.getEvaluatorUserType(), UserType.CROWDSOURCE)) {
			//获取创客
			CrowdsourceAccount account = new CrowdsourceAccount().selectOne(
					Condition.create().eq("crowdsourceid", evaluateFormBean.getEvaluatorUserId()));
			if (null == account) {
				return smartResult(StatusMsg.StatusMsg_601);
			}
			Evaluate evaluate = new Evaluate();
			BeanUtils.copyProperties(evaluateFormBean, evaluate);
			if (!evaluate.insert()) {
				return smartResult(StatusMsg.StatusMsg_199);
			}
			if (0 == account.getWhetherEvaluate()) {
				//将是否评价过标识修改
				account.setWhetherEvaluate(1);
				if (!account.updateById()) {
					return smartResult(StatusMsg.StatusMsg_199);
				}
			}

		} else {
			return smartResult(StatusMsg.StatusMsg_804);
		}

		//创客评价 是否推荐为"会"时推送，或星星评价为1,2,5 推送指定客服judy
		if (1 == evaluateFormBean.getWhetherRecommend() || 4 == evaluateFormBean.getWhetherRecommend() ||
				1 == evaluateFormBean.getAppraiseeGrade() || 2 == evaluateFormBean.getAppraiseeGrade() ||
				5 == evaluateFormBean.getAppraiseeGrade() || 1 == evaluateFormBean.getPlatformGrade() ||
				2 == evaluateFormBean.getPlatformGrade() || 5 == evaluateFormBean.getPlatformGrade()) {
			//处理消息
			noticeCustomer(evaluateFormBean);
		}
		return smartResult();

	}

	/**
	 * 通知指定客服
	 */
	public void noticeCustomer(SaveEvaluateFormBean evaluateFormBean) {

		String platformGradeCN = NumberToCN.numberToCN(evaluateFormBean.getPlatformGrade()) + "星";
		String appraiseGradeCN = NumberToCN.numberToCN(evaluateFormBean.getAppraiseeGrade()) + "星";
		String whetherRecommend = evaluateFormBean.covertWhether(evaluateFormBean.getWhetherRecommend());

		String info = "";//通知内容
		//评价方用户类型：1. 创客，2.企业，
		if (1 == evaluateFormBean.getEvaluatorUserType()) {
			MakerAccount makerAccount = new MakerAccount().selectOne(
					Condition.create().eq("userid", evaluateFormBean.getEvaluatorUserId()));
			Company company = new Company().selectOne(Condition.create().eq("compid", evaluateFormBean.getAppraiseeUserId()));
			if (company == null) {
				//组装消息内容
				info = String.format("创客【%s】评价平台%s，是否会推荐：%s，请在24小时内跟进并填写回访记录。", makerAccount.getRealname(), platformGradeCN, whetherRecommend);
			} else {
				//组装消息内容
				info = String.format("创客【%s】评价平台%s，企业【%s】%s，是否会推荐：%s，请在24小时内跟进并填写回访记录。", makerAccount.getRealname(), platformGradeCN,
						company.getCompname(), appraiseGradeCN, whetherRecommend);
			}
			//通知指定客服
			wxMessageService.notifyCustomerService(info);

		} else if (2 == evaluateFormBean.getEvaluatorUserType()) {
			MakerAccount makerAccount = new MakerAccount().selectOne(
					Condition.create().eq("userid", evaluateFormBean.getAppraiseeUserId()));
			Company company = new Company().selectOne(Condition.create().eq("compid", evaluateFormBean.getEvaluatorUserId()));
			//获取企业
			CompanyAccount account = new CompanyAccount().selectOne(Condition.create().eq("compid",
					evaluateFormBean.getEvaluatorUserId()));
			if (makerAccount == null) {
				//组装消息内容
				info = String.format("企业【%s】评价平台%s，是否会推荐：%s，请在24小时内跟进并填写回访记录。", company.getCompname(), platformGradeCN, whetherRecommend);
			} else {
				//组装消息内容
				info = String.format("企业【%s】评价平台%s，创客【%s】%s，是否会推荐：%s，请在24小时内跟进并填写回访记录。", company.getCompname(), platformGradeCN,
						makerAccount.getRealname(), appraiseGradeCN, whetherRecommend);
			}

			//通知商务经理
			String finalInfo = info;
			new Thread(() -> commonService.sendEnterpriseWeChatNotice(account.getBusinessManagerId(), null, 1, finalInfo)).start();
		}
	}

	public SmartResult queryWhetherEvaluate(BaseSimpleFormBean formBean) {
		if (null == formBean || 0 == formBean.getIdtype() || 0 == formBean.getUserid()) {
			return errorParamSmartResult();
		}
		//判断当前登录者用户类型
		if (1 == formBean.getIdtype()) {
			//获取标识
			MakerAccount account = new MakerAccount().selectOne(Condition.create().eq("userid", formBean.getUserid()));
			if (null == account) {
				return smartResult(StatusMsg.StatusMsg_201);
			}
			return smartResult(account.getWhetherEvaluate());
		} else if (2 == formBean.getIdtype()) {
			CompanyAccount account = new CompanyAccount().selectOne(Condition.create().eq("compid", formBean.getUserid()));
			if (null == account) {
				return smartResult(StatusMsg.StatusMsg_302);
			}
			return smartResult(account.getWhetherEvaluate());
		} else {
			return smartResult(StatusMsg.StatusMsg_804);
		}
	}

	public SmartResult noReminders(BaseSimpleFormBean formBean) {
		if (null == formBean || 0 == formBean.getIdtype() || 0 == formBean.getUserid()) {
			return errorParamSmartResult();
		}
		if (1 == formBean.getIdtype()) {
			//获取创客
			MakerAccount account = new MakerAccount().selectOne(Condition.create().eq("userid", formBean.getUserid()));
			if (null == account) {
				return smartResult(StatusMsg.StatusMsg_201);
			}
			account.setWhetherEvaluate(2);
			if (!account.updateById()) {
				return smartResult(StatusMsg.StatusMsg_199);
			}
			return smartResult();
		} else if (2 == formBean.getIdtype()) {
			//获取企业
			CompanyAccount account = new CompanyAccount().selectOne(Condition.create().eq("compid", formBean.getUserid()));
			if (null == account) {
				return smartResult(StatusMsg.StatusMsg_302);
			}
			account.setWhetherEvaluate(2);
			if (!account.updateById()) {
				return smartResult(StatusMsg.StatusMsg_199);
			}
			return smartResult();
		} else {
			return smartResult(StatusMsg.StatusMsg_804);
		}
	}
}
